package main

import (
	"bufio"
	"gitee.com/hufengjiu/go-zinx/znet"
	"log"
	"net"
	"os"
	"time"
)

func main() {
	conn, err := net.Dial("tcp", "127.0.0.1:9999")
	if err != nil {
		panic(err)
	}

	//scanner := os.Stdin
	f, err := os.Open("D:\\htdocs\\go\\src\\go-zinx\\demo\\v0.3\\data.dat")
	if err != nil {
		panic(err)
	}

	scanner := bufio.NewReader(f)

	go func() {
		dp := znet.NewDataPack()
		for {
			str, err := scanner.ReadString('\n')
			if err != nil {
				return
			}
			log.Printf("writer %s len %d", str, len(str))
			// 写出的时候,  需要指定写出长度
			m2 := znet.NewMessage(2, []byte(str))

			slice2, _ := dp.Pack(m2)

			_, err = conn.Write(slice2)
			if err != nil {
				return
			}
			time.Sleep(2 * time.Second)
		}

	}()
	dp := znet.NewDataPack()
	for {
		message, err := dp.UnPackFromReader(conn)
		if err != nil {
			panic(err)
		}
		log.Printf("read data %s len %d\n", message.GetData(), message.GetDataLen())
	}

}
